<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.24.0/axios.js"></script>
    <script src="./js/params.js"></script>

    <style>
        #app {
            width: 400px;
            margin: auto;
            margin-top: 100px;
        }
    </style>
</head>
<body>
    <div id="app">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="uname">
                <el-input v-model="ruleForm.uname" autocomplete="off"></el-input>
              </el-form-item>
          <el-form-item label="密码" prop="upassword">
            <el-input type="password" v-model="ruleForm.upassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="">
            <img :src="imgUrl" @click="getImageUrl">
          </el-form-item>
          <el-form-item label="验证码" prop="inputCode">
            <el-input @click="getCode" v-model="inputCode" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
    </div>
    <script>
        var Main = {
            data() {
                var validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        if (this.ruleForm.checkPass !== '') {
                            this.$refs.ruleForm.validateField('checkPass');
                        }
                        callback();
                    }
                };
                var validatePass2 = (rule, value, callback) => {
                    if (value === '') {
                    callback(new Error('请再次输入密码'));
                    } else if (value !== this.ruleForm.upassword) {
                    callback(new Error('两次输入密码不一致!'));
                    } else {
                    callback();
                    }
                };
                var validateName = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入用户名'));
                    } else {
                        callback();
                    }
                };
                return {
                    ruleForm: {
                        uname:"root",
                        upassword: 'root',
                        checkPass: '',
                    },
                    imgUrl:'',
                    inputCode:'',
                    code:'',
                    rules: {
                        uname: [
                            { validator: validateName, trigger: 'blur' }
                        ],
                        upassword: [
                            { validator: validatePass, trigger: 'blur' }
                        ],
                        checkPass: [
                            { validator: validatePass2, trigger: 'blur' }
                        ]
                    }
                };
            },
            methods: {
                submitForm(formName) {
                    if(this.inputCode.toUpperCase() != this.code){
                        this.$message.error('验证码错误');
                        return
                    }
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            var uname = this.ruleForm.uname;
                            var upassword = this.ruleForm.upassword
                            console.log(uname + upassword)
                            axios.get(`http://localhost:8001/user/login`,{
                                params:{
                                    uname:uname,
                                    upassword:upassword
                                }
                            })
                            .then(response=>{
                                console.log(response.data)
                                if(response.data.flag == true){
                                    console.log(response.data.data.token)
                                    console.log(response.data.data.user)
                                    sessionStorage.token = response.data.data.token
                                    sessionStorage.setItem("user",JSON.stringify(response.data.data.user))
                                    location.href = "index.html"
                                }else{
                                    this.open4();
                                }
                            })
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                },
                open4() {
                    duration:'1'
                    this.$message.error('用户名或密码错误');
                },
                getImageUrl(){
                    axios.get("http://localhost:8001/user/code")
                    .then(res=>{
                        console.log(res.data)
                        this.code = res.data
                        this.imgUrl = 'D://verifies//' + res.data + '.jpg'
                        console.log(this.imgUrl)
                    })
                }
            },
            created(){
                this.getImageUrl()
            }
        }
        var Ctor = Vue.extend(Main)
        new Ctor().$mount('#app')
    </script>
</body>
</html>